<template>
  <div>
    <el-form label-width="100px" :model="form" ref="form" :rules="rules" label-position="left">
      <el-form-item label="收货人" prop="addressname">
        <el-input v-model="form.addressname"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="telephone">
        <el-input v-model="form.telephone"></el-input>
      </el-form-item>
      <el-form-item label="地区" prop="area">
        <area-cascader class="area-cascader-wrap" :level="1" type="text" v-model="address" :data="pcaa"></area-cascader>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="form.address" type="text"></el-input>
      </el-form-item>
      <el-form-item style="text-align: right;">
        <el-button v-if="myAddress" type="primary" @click="onSave">修改收货地址</el-button>
        <el-button v-else type="primary" @click="onSave">添加收货地址</el-button>
        <el-button type="danger" @click="$emit('updata')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addAddress,updateAddress } from '@/api/user.js'
import 'vue-area-linkage/dist/index.css'
import { pca, pcaa } from 'area-data'
export default {
  name: 'AddAddress',
  props: ['myAddress'],
  data() {
    return {
      address: '',
      pca: pca,
      pcaa: pcaa,
      form: {
        addressname: this.myAddress.addressname || '',
        telephone: this.myAddress.telephone || '',
        userid: this.$store.state.user.profile.userId, // 用户id
        address: this.myAddress.address || '', // 详细地址
        area: this.myAddress.area || '',
        // addressid: Math.round(Math.random() * 10000) // 地址id
        addressid: this.myAddress.addressid || '' // 地址id
      },
      rules: {
        addressname: [{ required: true, message: '请输入收货人', trigger: 'blur' }],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入手机号', trigger: 'blur' }
        ],

        area: [{ required: true, message: '请输入地址', trigger: 'blur' }],

        address: [{ required: true, message: '请输入地址', trigger: 'blur' }]
      }
    }
  },
  watch: {
    address: function(value) {
      this.form.area = value.join('-')
    }
  },
  methods: {
    addArea() {
      console.log(11)
    },
    // 添加地址
    onSave() {
      this.$refs.form.validate(async valid => {
        if (valid && this.myAddress) {
          console.log(11);
          const res = await updateAddress({ ...this.form })
          console.log(res);
          if (res.data.code == '000') {
            this.$message.success('修改成功')
            this.$emit('updata')
          } else {
            this.$message.warning(res.data.msg)
          }
        } else {
          const res = await addAddress({ ...this.form })
          if (res.data.code == '000') {
            this.$message.success('添加成功')
            this.$emit('updata')
          } else {
            this.$message.warning(res.data.msg)
          }
        }
      })
    },
    onExit() {
      console.log(11)
    }
  }
}
</script>
<style lang="less" scoped>
.area-cascader-wrap {
  width: 100%;
  ::v-deep.large {
    width: 100%;
    line-height: 20px;
  }
}
</style>
